home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 110 / EnigmaAmiga110CD.iso / dalla rivista / host contacted / wbstartup+.lha / WBStartup+ / Source / WBStartup+OS3.5 / ProgressWindow.c < prev    next >
C/C++ Source or Header  |  2000-02-25  |  16KB  |  505 lines

  1. #include <exec/types.h>
  2. #include <graphics/gfx.h>
  3. #include <proto/intuition.h>
  4. #include <proto/gadtools.h>
  5. #include <proto/graphics.h>
  6. #include <proto/dos.h>
  7. #include <proto/exec.h>
  8. #include <proto/icon.h>
  9. #include <proto/iffparse.h>
  10. #include <proto/datatypes.h>
  11. #include <proto/layers.h>
  12. #include <prefs/wbpattern.h>
  13. #include <prefs/prefhdr.h>
  14. #include <graphics/gfxmacros.h>
  15. #include <math.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <exec/memory.h>
  19. #include <datatypes/datatypesclass.h>
  20. #include <datatypes/pictureclass.h>
  21.  
  22. #include "ProgressWindow.h"
  23. #include "WBStartup+.h"
  24.  
  25. #define winwidth 400
  26. #define winheight 80
  27. #define BARHEIGHT 12
  28. #define XOFFSET 75
  29.  
  30. #ifndef min
  31. #define min(x, y)  ((x) < (y) ? (x) : (y))
  32. #endif
  33.  
  34. #ifndef max
  35. #define max(x, y) (! (min (x, y)))
  36. #endif
  37.  
  38. #include <clib/alib_protos.h>
  39. #include <clib/icon_protos.h>
  40. #include <workbench/icon.h>
  41.  
  42. static UWORD BackgroundPen, HighlightTextPen, ShinePen, ShadowPen, FillPen;
  43. //static WORD s_fill = -1;
  44. ULONG __chip Crosshatch = 0x5555AAAA;
  45. //static struct WBStartup *wbarg=NULL;
  46.  
  47. struct ProgressWindowData *CreateProgressWindow(struct WBStartupPrefs *prefs)
  48. {
  49.   ULONG scrwidth=640,scrheight=200;
  50.   struct Rectangle rect;
  51.   LONG screen_modeID;
  52.   void *vi;
  53.   char buffer[7],*TitleText="Amiga Workbench \0\0\0\0\0\0\0";
  54.   struct IntuiText iText = { 2,0,0,0,0,NULL,NULL,NULL};
  55.   struct DrawInfo *dri;
  56.   struct Screen *scr;
  57.  
  58.   struct DiskObject *diskobj=NULL;  /* This must be initialized to NULL */
  59.   struct Process *process;
  60.   char buf[200];
  61.   struct Image *image;
  62.   struct ProgressWindowData *windata;
  63.   BOOL success=FALSE;
  64.  
  65.   if (windata = AllocVec(sizeof(struct ProgressWindowData),MEMF_PUBLIC))
  66.   {
  67.     windata->bitmap=NULL;
  68.     windata->iconundobitmap=NULL;
  69.     windata->filenameundobitmap=NULL;
  70.  
  71.     if (scr=LockPubScreen(prefs->PubScreenName))
  72.     {
  73.       if (prefs->BackgroundType!=NONE)
  74.         LoadBitmap(prefs,windata,scr);
  75.  
  76.       /********************************************/
  77.       /* Determine the Size of the visible screen */
  78.       /********************************************/
  79.       screen_modeID = GetVPModeID(&(scr->ViewPort));
  80.       if (screen_modeID != INVALID_ID)
  81.       {
  82.         if (QueryOverscan(screen_modeID, &rect, OSCAN_TEXT))
  83.         {
  84.           scrwidth  = rect.MaxX - rect.MinX + 1;
  85.           scrheight = rect.MaxY - rect.MinY + 1;
  86.  
  87.           scrwidth  = min(scrwidth,scr->Width);
  88.           scrheight = min(scrheight,scr->Height);
  89.         }
  90.       }
  91.  
  92.       /*******************************/
  93.       /* Get WBStartup+'s Icon image */
  94.       /*******************************/
  95.       if (process = (struct Process *)FindTask(NULL))
  96.       {
  97.         NameFromLock(process->pr_HomeDir,buf,199);
  98.         AddPart(buf,process->pr_Task.tc_Node.ln_Name,199);
  99.  
  100.         if (diskobj=(GetIconTags (buf, TAG_DONE)))  // We release the Disk Object down below!
  101.           image = (struct Image *)diskobj->do_Gadget.GadgetRender;
  102.         else
  103.           ShowRequester ("arse");
  104.       }
  105.  
  106.       if (windata->win = OpenWindowTags(NULL,
  107.           WA_Left,   (scrwidth-winwidth)/2,
  108.           WA_Top,    (scrheight-winheight)/2,
  109.           WA_Width, winwidth,//(TextLength(&scr->RastPort,"Program Name",12)*2) + (TextLength(&scr->RastPort,"Priority",8)*2) + (2 * OUTSIDEBORDER) + scr->WBorLeft + scr->WBorRight,
  110.           WA_Height, winheight,
  111.           WA_AutoAdjust, TRUE,
  112.           WA_Activate, (prefs->Interactive) ? 1 : 0,
  113.           WA_IDCMP,  (prefs->Interactive) ? IDCMP_VANILLAKEY : 0L,
  114.           WA_Flags,  WFLG_SMART_REFRESH | WFLG_ACTIVATE,
  115.           WA_PubScreen, scr,
  116.           WA_PubScreenFallBack, "Workbench",
  117.           WA_SuperBitMap, windata->bitmap,
  118.           TAG_END))
  119.       {
  120.         /* Set Global Pen Numbers */
  121.         if (dri = GetScreenDrawInfo(scr))
  122.         {
  123.           BackgroundPen = dri->dri_Pens[BACKGROUNDPEN];
  124.           HighlightTextPen = dri->dri_Pens[HIGHLIGHTTEXTPEN];
  125.           ShinePen = dri->dri_Pens[SHINEPEN];
  126.           ShadowPen = dri->dri_Pens[SHADOWPEN];
  127.           FillPen = dri -> dri_Pens [FILLPEN];
  128.           FreeScreenDrawInfo( scr, dri );
  129.         }
  130.  
  131.         // NEW: Set the pattern for the progress bar
  132.         // SetFill (prefs);
  133.  
  134.         if (vi=GetVisualInfo(scr,TAG_DONE))
  135.         {
  136.           DrawBevelBox(windata->win->RPort, XOFFSET, 34, windata->win->Width-150, BARHEIGHT, GTBB_Recessed, TRUE, GT_VisualInfo, vi, TAG_DONE);  /* Draw Progress Indicator Box */
  137.           FreeVisualInfo(vi);
  138.         }
  139.  
  140.         /************************/
  141.         /* Write text to window */
  142.         /************************/
  143.         if (GetVar("Workbench",buffer,7,0)>=0)
  144.           strcat(TitleText,buffer);
  145.         iText.IText = TitleText;
  146.         iText.FrontPen = ShadowPen;
  147.         PrintIText(windata->win->RPort, &iText, ((windata->win->Width-IntuiTextLength(&iText))/2)+1, 16);
  148.         iText.FrontPen = HighlightTextPen;
  149.         PrintIText(windata->win->RPort, &iText, (windata->win->Width-IntuiTextLength(&iText))/2, 15);
  150.  
  151.         SetAfPt(windata->win->RPort, NULL, 0);
  152.  
  153.         /********************************/
  154.         /* Make our undo buffer bitmaps */
  155.         /********************************/
  156.         if (windata->bitmap)
  157.         {
  158.           /* icon undo bitmap */
  159.           if (windata->iconundobitmap=AllocBitMap(XOFFSET-windata->win->BorderRight,winheight-(windata->win->BorderTop+windata->win->BorderBottom),GetBitMapAttr(windata->bitmap,BMA_DEPTH),GetBitMapAttr(windata->bitmap,BMA_FLAGS),NULL))
  160.             BltBitMap(windata->bitmap,winwidth-XOFFSET,windata->win->BorderTop,windata->iconundobitmap,0,0,XOFFSET-windata->win->BorderRight,winheight-(windata->win->BorderTop+windata->win->BorderBottom),0x0C0,0xFF,NULL);
  161.           /* filename undo bitmap */
  162.           if (windata->filenameundobitmap=AllocBitMap(winwidth-(2*XOFFSET),winheight-34-BARHEIGHT-windata->win->BorderBottom,GetBitMapAttr(windata->bitmap,BMA_DEPTH),GetBitMapAttr(windata->bitmap,BMA_FLAGS),NULL))
  163.             BltBitMap(windata->bitmap,XOFFSET,34+BARHEIGHT,windata->filenameundobitmap,0,0,winwidth-(2*XOFFSET),winheight-34-BARHEIGHT-windata->win->BorderBottom,0x0C0,0xFF,NULL);
  164.         }
  165.  
  166.         success=TRUE;
  167.       }
  168.  
  169.       if (diskobj)
  170.       {
  171.         //DrawImage(windata->win->RPort,image,((XOFFSET-windata->win->BorderLeft-image->Width)/2)+windata->win->BorderLeft,((80-image->Height)/2));
  172.         LayoutIconA (diskobj, scr, NULL);
  173.         DrawIconState (windata->win->RPort, diskobj, NULL,
  174.                       ((XOFFSET-windata->win->BorderLeft-image->Width)/2)+windata->win->BorderLeft,
  175.                       ((80-image->Height)/2), IDS_NORMAL, ICONDRAWA_Borderless, TRUE, NULL);
  176.         FreeDiskObject(diskobj);
  177.       }
  178.  
  179.       UnlockPubScreen(NULL,scr);
  180.     }
  181.   }
  182.  
  183.   if (!success)
  184.   {
  185.     FreeVec(windata);
  186.     windata=NULL;
  187.   }
  188.  
  189.   return(windata);
  190. }
  191.  
  192. void CloseProgressWindow(struct ProgressWindowData *data)
  193. {
  194.   CloseWindow(data->win);
  195.   if (data->bitmap)
  196.     FreeBitMap(data->bitmap);
  197.   if (data->iconundobitmap)
  198.     FreeBitMap(data->iconundobitmap);
  199.   if (data->filenameundobitmap)
  200.     FreeBitMap(data->filenameundobitmap);
  201.   FreeVec(data);
  202. }
  203.  
  204. void UpdateProgressBar(struct Window *win, int current, int total, const WORD fill_pattern)
  205. {
  206.   int x;
  207.   x = ((win->Width-(2*(XOFFSET+2)))*current/total)+(XOFFSET+2);
  208.  
  209.   if (fill_pattern == -1) // old style
  210.     {
  211.       SetAfPt(win->RPort,(UWORD *)&Crosshatch,1);
  212.       SetBPen(win->RPort,ShinePen);
  213.       SetAPen(win->RPort,FillPen);
  214.     }
  215.   else
  216.     SetAPen (win -> RPort, fill_pattern);
  217.  
  218.   switch (fill_pattern)
  219.     {
  220.       case BACKGROUNDPEN:
  221.         SetAPen (win -> RPort, BackgroundPen);
  222.         break;
  223.  
  224.       case FILLPEN:
  225.         SetAPen (win -> RPort, FillPen);
  226.         break;
  227.  
  228.       case HIGHLIGHTTEXTPEN:
  229.         SetAPen (win -> RPort, HighlightTextPen);
  230.         break;
  231.  
  232.       case SHINEPEN:
  233.         SetAPen (win -> RPort, ShinePen);
  234.         break;
  235.  
  236.       case SHADOWPEN:
  237.         SetAPen (win -> RPort, ShadowPen);
  238.         break;
  239.  
  240.       default:
  241.         // leave fill as it is - crosshatch
  242.         SetAfPt(win->RPort,(UWORD *)&Crosshatch,1);
  243.         SetBPen(win->RPort,ShinePen);
  244.         SetAPen(win->RPort,FillPen);
  245.         break;
  246.     }
  247.  
  248.  
  249.   
  250.   RectFill(win->RPort,(XOFFSET+2),35,x,35+BARHEIGHT-3);
  251. }
  252.  
  253. void ShowIconImage(struct ProgressWindowData *data, struct DiskObject *disk_obj_p)
  254. {
  255.   struct Region *reg;
  256.   struct Region *oldreg;
  257.   struct Rectangle rect;
  258.   struct Image *image = (struct Image *)disk_obj_p->do_Gadget.GadgetRender;
  259.  
  260.   if (data->iconundobitmap)
  261.     BltBitMapRastPort(data->iconundobitmap,0,0,data->win->RPort,winwidth-XOFFSET,data->win->BorderTop,XOFFSET-data->win->BorderRight,winheight-data->win->BorderTop-data->win->BorderBottom,0x0C0);
  262.   else
  263.   {
  264.     SetAfPt(data->win->RPort,NULL,0);
  265.     SetAPen(data->win->RPort,BackgroundPen);
  266.     RectFill(data->win->RPort,winwidth-XOFFSET,data->win->BorderTop,winwidth-data->win->BorderRight-1,winheight-data->win->BorderBottom-1);
  267.   }
  268.  
  269.   rect.MinX=winwidth-XOFFSET;
  270.   rect.MinY=data->win->BorderTop;
  271.   rect.MaxX=winwidth-data->win->BorderRight-1;
  272.   rect.MaxY=winheight-data->win->BorderBottom-1;
  273.   if (reg=NewRegion())
  274.   {
  275.     OrRectRegion(reg,&rect);
  276.  
  277.     oldreg=InstallClipRegion(data->win->WLayer,reg);
  278.  
  279.     //DrawImage(data->win->RPort,image,(400-XOFFSET)+((XOFFSET-image->Width)/2),((80-image->Height)/2));
  280.  
  281.     DrawIconState (data->win->RPort, disk_obj_p, NULL,
  282.                   (400-XOFFSET)+((XOFFSET-image->Width)/2),
  283.                   ((80-image->Height)/2), IDS_NORMAL, ICONDRAWA_Borderless, TRUE, NULL);
  284.  
  285.  
  286.     InstallClipRegion(data->win->WLayer,oldreg);
  287.  
  288.     DisposeRegion(reg);
  289.   }
  290.  
  291. }
  292.  
  293.  
  294.  
  295.  
  296.  
  297. BOOL LoadBitmap(struct WBStartupPrefs *prefs, struct ProgressWindowData *data, struct Screen *scr)
  298. {
  299.   /* Load the picture file with datatypes.library, copy the resulting bitmap and release the file */
  300.  
  301.   struct FrameInfo *o;   /* This is our datatype object */
  302.   struct BitMapHeader *bmhd=NULL;
  303.   struct BitMap       *bitmap;
  304.   struct gpLayout gpl;
  305.   char   pathname[200];
  306.   BOOL   success=FALSE;
  307.  
  308.   struct IFFHandle *iffhandle;
  309.   struct CollectionItem *ci;
  310.   LONG ifferror;
  311.  
  312.   ULONG width, height, xoff, yoff, mapwidth, mapheight;
  313.  
  314.   /***********************************************************************/
  315.   /* Get the filename of the graphics picture to use from WBPattern.prefs*/
  316.   /***********************************************************************/
  317.   if (!prefs->BackgroundFilename[0])
  318.   {
  319.     if (iffhandle = AllocIFF())
  320.     {
  321.       if (iffhandle->iff_Stream = (LONG)Open("ENV:sys/WBPattern.prefs",MODE_OLDFILE))
  322.       {
  323.         InitIFFasDOS(iffhandle);
  324.         if ((ifferror = OpenIFF(iffhandle,IFFF_READ)) == 0)
  325.         {
  326.           CollectionChunk(iffhandle, ID_PREF, ID_PTRN);
  327.  
  328.           for(;;)
  329.           {
  330.  
  331.           ifferror=ParseIFF(iffhandle, IFFPARSE_STEP);
  332.           
  333.           if (ifferror == IFFERR_EOC)
  334.             continue;
  335.           else if (ifferror)
  336.             break;
  337.  
  338.           if (ci = FindCollection(iffhandle, ID_PREF, ID_PTRN))
  339.             do {
  340.               if ((!(((struct WBPatternPrefs *)ci->ci_Data)->wbp_Flags & WBPF_PATTERN)) && (((struct WBPatternPrefs *)ci->ci_Data)->wbp_Which == prefs->BackgroundType-1))
  341.               {
  342.                 strncpy(pathname,(char *)(ci->ci_Data)+sizeof(struct WBPatternPrefs),((struct WBPatternPrefs *)ci->ci_Data)->wbp_DataLength);
  343.                 pathname[((struct WBPatternPrefs *)ci->ci_Data)->wbp_DataLength]=0;
  344.               }
  345.               ci = ci->ci_Next;
  346.             } while (ci);
  347.           }
  348.           CloseIFF(iffhandle);
  349.         }
  350.         Close(iffhandle->iff_Stream);
  351.       }
  352.       FreeIFF(iffhandle);
  353.     }
  354.   }
  355.   else
  356.     strcpy(pathname,prefs->BackgroundFilename);
  357.  
  358.   /*******************************************/
  359.   /* Read the graphics file and get a bitmap */
  360.   /*******************************************/
  361.   if (pathname[0])   /* Do we have a file name to use ? */
  362.   {
  363.     if (o = (struct FrameInfo *)NewDTObject(pathname,
  364.                                             DTA_SourceType,DTST_FILE,
  365.                                             DTA_GroupID,GID_PICTURE,
  366.                                             PDTA_Remap,TRUE,
  367.                                             PDTA_Screen,scr,
  368.                                             TAG_DONE))
  369.     {
  370.       Msg message = (Msg) &gpl;
  371.  
  372.       gpl.MethodID = DTM_PROCLAYOUT;
  373.       gpl.gpl_GInfo = NULL;
  374.       gpl.gpl_Initial = 1;
  375.  
  376.       if (DoMethodA((Object *)o, message))
  377.         GetDTAttrs((Object *)o,
  378.               PDTA_BitMapHeader,&bmhd,
  379.               PDTA_BitMap,&bitmap,
  380.               TAG_DONE);
  381.  
  382.  
  383.       /************************************************************/
  384.       /*   Tile the bitmap into a bitmap the size of our window   */
  385.       /************************************************************/
  386.       if (data->bitmap=AllocBitMap(winwidth,winheight,GetBitMapAttr(bitmap,BMA_DEPTH),GetBitMapAttr(bitmap,BMA_FLAGS),NULL))
  387.       {
  388.         mapwidth = bmhd->bmh_Width;
  389.         mapheight = bmhd->bmh_Height;
  390.         yoff = 0;
  391.         while (yoff < winheight)
  392.         {
  393.           xoff=0;
  394.           height = min(mapheight,winheight-yoff);
  395.           while (xoff < winwidth)
  396.           {
  397.             width = min(mapwidth,winwidth-xoff);
  398.             BltBitMap(bitmap,0,0,data->bitmap,xoff,yoff,width,height,0x0C0,0xFF,NULL);
  399.             xoff += width;
  400.           }
  401.           yoff += height;
  402.         }
  403.         success=TRUE;
  404.       }
  405.  
  406.       DisposeDTObject((Object *)o);
  407.     }
  408.   }
  409.  
  410.   return(success);
  411. }
  412.  
  413.  
  414. BOOL InteractiveRunProgram(struct Window *win)
  415. {
  416.   BOOL runprogram=FALSE;
  417.   struct IntuiMessage *msg;
  418.   BOOL done=FALSE;
  419.  
  420.   while (!done)
  421.   {
  422.     Wait(1L<<win->UserPort->mp_SigBit);
  423.  
  424.     while (msg = (struct IntuiMessage *)GetMsg(win->UserPort))
  425.     {
  426.       if (msg->Class == IDCMP_VANILLAKEY)
  427.         if (msg->Code=='Y' || msg->Code=='y')
  428.         {
  429.           done=TRUE;
  430.           runprogram = TRUE;
  431.         }
  432.         else if (msg->Code=='N' || msg->Code=='n')
  433.         {
  434.           done=TRUE;
  435.           runprogram = FALSE;
  436.         }
  437.       ReplyMsg((struct Message *)msg);
  438.     }
  439.   }
  440.  
  441.   return(runprogram);
  442. }
  443.  
  444. void DisplayProgramName(struct ProgressWindowData *data, char *name, BOOL query)
  445. {
  446.   struct IntuiText iText = { 2,0,0,0,0,NULL,NULL,NULL};
  447.   char *outputbuffer;
  448.  
  449.   /* Fill the text area with our bitmap or blank space */
  450.   if (data->filenameundobitmap)
  451.     BltBitMapRastPort(data->filenameundobitmap,0,0,data->win->RPort,XOFFSET,34+BARHEIGHT,winwidth-(2*XOFFSET),winheight-34-BARHEIGHT-data->win->BorderBottom,0x0C0);
  452.   else
  453.   {
  454.     SetAfPt(data->win->RPort,NULL,0);
  455.     SetAPen(data->win->RPort,BackgroundPen);
  456.     RectFill(data->win->RPort,XOFFSET,34+BARHEIGHT,winwidth-XOFFSET-1,winheight-data->win->BorderBottom-1);
  457.   }
  458.  
  459.   if (outputbuffer = AllocVec(strlen(name)+3,MEMF_PUBLIC))
  460.   {
  461.     strcpy(outputbuffer,name);
  462.     if (query)
  463.       strcat(outputbuffer," ?");
  464.     iText.IText = outputbuffer;
  465.     iText.FrontPen = ShadowPen;
  466.     PrintIText(data->win->RPort, &iText, ((data->win->Width-IntuiTextLength(&iText))/2)+1, 34+BARHEIGHT+((data->win->Height-34-BARHEIGHT-data->win->WScreen->Font->ta_YSize)/2)+2+1);
  467.     iText.FrontPen = HighlightTextPen;
  468.     PrintIText(data->win->RPort, &iText, (data->win->Width-IntuiTextLength(&iText))/2, 34+BARHEIGHT+((data->win->Height-34-BARHEIGHT-data->win->WScreen->Font->ta_YSize)/2)+2);
  469.     FreeVec(outputbuffer);
  470.   }
  471. }
  472.  
  473. /*
  474. void SetFill (struct WBStartupPrefs *prefs_p)
  475. {
  476.   switch (prefs_p -> FillGaugeType)
  477.     {
  478.       case BACKGROUNDPEN:
  479.         s_fill = BackgroundPen;
  480.         break;
  481.  
  482.       case FILLPEN:
  483.         s_fill = FillPen;
  484.         break;
  485.  
  486.       case HIGHLIGHTTEXTPEN:
  487.         s_fill = HighlightTextPen;
  488.         break;
  489.  
  490.       case SHINEPEN:
  491.         s_fill = ShinePen;
  492.         break;
  493.  
  494.       case SHADOWPEN:
  495.         s_fill = ShadowPen;
  496.         break;
  497.  
  498.       default:
  499.         // leave fill as it is - crosshatch
  500.         break;
  501.     }
  502.  
  503. }
  504. */
  505.